-- 设置全局变量和函数，以支持IDE自身的调试
_G.CJ_NVIM_DEBUG = false
function _G.DEBUG(msg)
	if (_G.CJ_NVIM_DEBUG) then
		print("[DEBUG]" .. toString(msg))
	end
end

local cjlogo = {
        " ██████╗ █████╗ ███╗   ██╗ ██████╗      ██╗██╗███████╗",
        "██╔════╝██╔══██╗████╗  ██║██╔════╝      ██║██║██╔════╝",
        "██║     ███████║██╔██╗ ██║██║  ███╗     ██║██║█████╗  ",
        "██║     ██╔══██║██║╚██╗██║██║   ██║██   ██║██║██╔══╝  ",
        "╚██████╗██║  ██║██║ ╚████║╚██████╔╝╚█████╔╝██║███████╗",
        " ╚═════╝╚═╝  ╚═╝╚═╝  ╚═══╝ ╚═════╝  ╚════╝ ╚═╝╚══════╝",
        "                                                      ",
}

-- 最近文件列表（可用插件或全局变量记录）
local recent_files = vim.g.snacks_recent_files or {}
local recent_dirs = vim.g.snacks_recent_dirs or {}


function get_recent_path()
	local files = Snacks.dashboard.oldfiles({})
	print(files)
	return "recent_path"
end

function open_tree_or_file(path)
	if path == nil then
		_G.DEBUG("no path or file!")
		return
	end
	_G.DEBUG(path)
	if vim.fn.isdirectory(path) then
		_G.DEBUG(path)
		-- require("snacks").tree.open({
		-- 	root = path,
		-- 	side = "left"
		-- })
	else 
		vim.cmd("edit " .. vim.fn.fnameescape(file))
	end
end

-- VimEnter: 如果只有一个文件夹参数，则打开picker
-- vim.api.nvim_create_autocmd("VimEnter", {
-- 	callback = function()
-- 		local argvs = vim.fn.argv()
-- 		print(vim.inspect(argvs))
-- 		local bufs = vim.api.nvim_list_bufs()
-- 		for _, burnr in iparis(bufs) do
-- 			print(vim.api.nvim_buf_get_name(burnr))
-- 		end
-- 		local dir = nil
-- 		for i = 1, #argvs do
-- 			if argvs[i]:match("--debug") then
-- 				_G.CJ_NVIM_DEBUG = true
-- 			elseif i > 1 and not argvs[i]:match('^%-') then
-- 				dir = argvs[i]
-- 				print(argvs[i])
-- 			end
-- 		end
-- 		if dir then
-- 			dir = get_recent_path()
-- 		end
-- 		open_tree_or_file(dir)
-- 	end
-- })

local dashboard = { 
	preset = {
		header = table.concat(cjlogo, "\n")
	},
	sections = {
		{ section = "header" },
		{ section = "keys", gap = 1, padding = 1 },
		{ padding = 3, pane = 2},
		{ title = "Workspace", icon = " ", pane = 2, key='w'},
		function() -- workspace
			local cmds = {
				{	
					file = vim.fn.fnamemodify(".", ":~"),
          			action = nil,
          			icon = " ",
          			enabled = true,
        		},
				{
          			file = vim.fn.fnamemodify(".", ":~"),
          			action = function()
           				
          			end,
          			icon = " ",
          			enabled = true,
        		},
			}
			local items = vim.tbl_map(function(cmd)
				return vim.tbl_extend("force", {
					pane = 2,
					ttl = 5 * 60,
					indent = 2,
				}, cmd)
			end, cmds)
			-- print(vim.inspect(items))
			return items
		end,
		{ padding = 2, pane = 2},
		{ title = "Projects", icon = " ", height = 1, pane = 2, key = 'p'},
		function() -- projects
			local cmds = {
				{	
					file = vim.fn.fnamemodify(".", ":~"),
          			action = nil,
          			icon = " ",
          			enabled = true,
        		},
				{
          			file = vim.fn.fnamemodify(".", ":~"),
          			action = function()
           				
          			end,
          			icon = " ",
          			enabled = true,
        		},
			}
			local items = vim.tbl_map(function(cmd)
				return vim.tbl_extend("force", {
					pane = 2,
					ttl = 5 * 60,
					indent = 2,
				}, cmd)
			end, cmds)
			-- print(vim.inspect(items))
			return items
		end,
		{ padding = 2, pane = 2},
		{ icon = " ", title = "Recent Files", section = "recent_files", indent = 2, padding = 1, pane = 2},
		{ section = "startup" },
	}
}

return {
	"folke/snacks.nvim",
	priority = 1000,
	lazy = false,
	dependencies = {"nvim-tree/nvim-web-devicons"},
	---@type snacks.Config
	opts = {
		bigfile = { enabled = true },
		quickfile = { enabled = true },
		terminal = { enabled = true },
		explorer = { enabled = true },
    	indent = { enabled = true },
    	input = { enabled = true },
    	picker = { enabled = true },
    	notifier = { enabled = true },
    	quickfile = { enabled = true },
    	scope = { enabled = true },
    	scroll = { enabled = true },
    	statuscolumn = { enabled = true },
    	words = { enabled = true },
		dashboard = dashboard,
	}	
}
